package com.example.controller;

import com.example.pojo.Administer;
import com.example.pojo.Book;
import com.example.pojo.Guest;
import com.example.service.GuestService;
import org.apache.logging.log4j.core.tools.picocli.CommandLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 名称GuestController
 * 描述discribe GuestController
 *
 * @version 1.0
 * @author:wanghao
 * @datatime:2023,04,16,16,44
 */
@Controller
public class GuestController {
    @Autowired
    private GuestService guestService;

    @RequestMapping("/GuestLoginPage")
    public String GuestLoginPage(){return "GuestLoginPage";}

    @ResponseBody
    @RequestMapping("/GuestLogin")
    public String GuestLogin(@RequestParam(value = "name")String name,@RequestParam(value = "passwd")String passwd)
    {
        if (guestService.CheckGuestPasswd(name, passwd).equals("true"))
            return "Succeed";
        else return "something wrong";
    }
    @RequestMapping("GuestRegisterPage")
    public String RegisterGuestPage(){return "GuestRegisterPage";}

    @RequestMapping("/GuestRegister")
    @ResponseBody
    public String RegisterGuest(Guest guest){
        int i= guestService.InsertGuest(guest);
        if (i>0)   {return "Succeed";}
        else {return "something wrong";}
    }

    @RequestMapping("FindBookByID")
    public  String FindBookByIDPage(){return "FindBookByIDPage";}

    @RequestMapping("FindBookByid")
    @ResponseBody
    public Book FindBookByID(Integer ID){
        System.out.println(guestService.FindBookByID(ID));
        return guestService.FindBookByID(ID);
    }

    @RequestMapping("FindBookByStr")
    public String FindBookByStrPage(){return "FindBookByStrPage";}

    @RequestMapping("FindBookBystr")
    @ResponseBody
    public List<Book> FindBookByStr(String searchStr)
    {
        System.out.println((guestService.FindBookByStr(searchStr)));
        return guestService.FindBookByStr(searchStr);
    }

    @RequestMapping("FindAllBookPage")
    public String FindAllBookPage(){return "FindAllBookPage";}

    @RequestMapping("FindAllBook")
    @ResponseBody
    public List<Book> FindAllBook(){
         return guestService.FindAllBook();
    }

//    @RequestMapping("BorrowBook")
//    @ResponseBody
//    public String BorrowBookByID(Book book){
//        Integer bookID = book.getID();
//        if (guestService.FindBookStatusByID(bookID).equals("true"))//说明此时有书可以借
//        {
//            int i = guestService.UpdateBook(book);
//            if (i>0)
//            {
//                List<Book> bookList = guestService.FindAllBook();
//                return "borrow";
//            }
//            else return "false";
//        }
//        else return "already borrowed";
//    }
//
//    @RequestMapping("ReturnBook")
//    @ResponseBody
//    public String ReturnBookByID(Book book) {
//        List<Book> bookList = guestService.FindAllBook();
//        Boolean IDExist = null;
//        if (bookList.stream().filter(w-> w.getBookID()== book.getBookID()).findAny().isPresent())//判断还的书是否在数据库中
//            IDExist = true;
//        else IDExist=false;
//        if (IDExist) {
//            int i = guestService.UpdateBook(book);
//            if (i > 0)
//                return "return book";
//            else return "fall to return book";
//        } else return "图书馆没这本书，可以选择插入";
//    }

}
